google.load("jquery", "1.4.4");
google.load("elements", "1", {
	 packages: "transliteration, keyboard"
});
    
var prefs = new gadgets.Prefs();
    
var lang = prefs.getString("lang");
var klang = prefs.getString("klang");
    
gadgets.util.registerOnLoadHandler(init);
    
var control;
var just = true;
    
function init() {
	
	var tabs = new gadgets.TabSet("trl");
    	
	tabs.addTab("Trasnliteration", {
	    contentContainer: _("trl_tab"),
	    callback: onTranslitTab, 
	    tooltip: "Transliterate your text"
	});
        
	tabs.addTab("Virtual_Keyboard", { 
		contentContainer: _("vkey_tab"),
		callback: onVkeyboardTab, 
		tooltip: "Use virtual keyboard"
	});
	
	$("#cb_on").click(trToggle);
};

function onTranslitTab() {
	
    var options = {
    		sourceLanguage: "en",
    		destinationLanguage: google.elements.transliteration.SupportedDestinationLanguages.ALL,
    		shortcutKey: "ctrl+g",
    		transliterationEnabled: true
    };

    control =  new google.elements.transliteration.TransliterationControl(options);
    
	createOptions("trl_select", google.elements.transliteration.LanguageCode, 
		    function (html) {
			    $("#trl_select_frm").html(html);
			    
			    setSelected("trl_select", lang, function(tolang) {
			        control.setLanguagePair(
			        	    google.elements.transliteration.LanguageCode.ENGLISH,
			        	    tolang);
			    });
			    
			    $("#trl_select").change(function(event) {
			    	
			    	var tolang = event['target'].options[event['target'].selectedIndex].value;
			    	 
			    	prefs.set("lang", tolang);
			    	
			        control.setLanguagePair(
			        	    google.elements.transliteration.LanguageCode.ENGLISH,
			        	    tolang);
			    });
	});
    
    control.makeTransliteratable(["trl_txt" ]);

    if (!just) {
    	$("#kbd").hide();
    	gadgets.window.adjustHeight();
    }
	just = false;
};

var kbd;
    
function onVkeyboardTab() {
	
	createOptions("vkey_select", google.elements.keyboard.LayoutCode, 
	    function (html) {
		    $("#vkey_select_frm").html(html);
		    
		    setSelected("vkey_select", klang, function(tolang) {
		        kbd = new google.elements.keyboard.Keyboard([tolang], ['vkey_txt']);
		        
			});
		    
			$("#vkey_select").change(function(event) {
			    	
			    var tolang = event['target'].options[event['target'].selectedIndex].value;
			    	 
			    prefs.set("lang", tolang);
			    	
			    kbd = new google.elements.keyboard.Keyboard([tolang], ['vkey_txt']);

		    });
	});
    kbd.setVisible(true);

    gadgets.window.adjustHeight();
}

function createOptions(id, list, callback) {
	
	var html = "<select id =" + id + ">";
	  
	$.each(list, function(index, value) {
		html += "<option value='" + value + "'>" + index + "</option>";
	});
		  
	html += "</select>";
	
	callback(html);
};
    
    function setSelected(id, val, callback) {
    	
    	var sel = _(id);
    	
    	for (var i = 1; i < sel.length; i++) {
    		if(sel.options[i].value == val) {
    			sel.selectedIndex = i;
    		}
    	};
    	
    	callback(val);
    };

    function trToggle() {
    	control.toggleTransliteration();
    };

    function setLang(dropdown) {
    	
    	var tolang = dropdown.options[dropdown.selectedIndex].value;
 
    	prefs.set("lang", tolang);
    	
        control.setLanguagePair(
        	    google.elements.transliteration.LanguageCode.ENGLISH,
        	    tolang);
    };
    
function _(id) {
	return document.getElementById(id);
};